package cn.com.jrr.core.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.dao.impl.BaseDaoImpl;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.PageModel;
import cn.com.jrr.core.model.TestModel;

@Service
public class UserService{
	@Resource
	private BaseDaoImpl<TestModel> baseDaoImpl;
	@Autowired
	PageModel<TestModel> pageModel;

	/**
	 * 查询数据
	 * */
	public DataTableResponse<TestModel> getAllUser(DataTableRequest dataTableRequest){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="select * from test1 order by id desc";
		Object [] obj=new String[]{"id","descr"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<TestModel> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,TestModel.class);
		return  dataTableResponse;
	}
	/**
	 * 添加数据
	 * */
	public int add(TestModel testModel){
		int res=(int) baseDaoImpl.updateBySql("insert into test1 (descr) values (?)",testModel.getDescr());
		return  res;
	}
	/**
	 * 获取一条数据
	 * */
	public TestModel get(Integer id){
		return  baseDaoImpl.get(TestModel.class, id);
	}
	/**
	 * 修改数据
	 * */
	public int update(TestModel testModel){
		return  baseDaoImpl.updateBySql("update test1 set descr=? where id=?", new Object[]{testModel.getDescr(),testModel.getId()});
	}
	/**
	 * 删除数据
	 * */
	public int delete(Integer id){
		return  baseDaoImpl.updateBySql("delete from test1 where id=?", new Object[]{id});
	}



}
